import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useUserStore} from '@/stores/user'

const service = axios.create({
    baseURL: 'http://218.93.212.70:23001', // url = base url + request url
    timeout: 1000 * 30 // request timeout
})
service.interceptors.request.use(
    config => {
        config.headers['token'] = localStorage.getItem('token')
        return config
    },
    error => {
        // do something with request error
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code === 0 || !res.code) {
            return res
        } else {
            ElMessage({
                message: res.err || 'Error',
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject('error')
        }
    },
    error => {
        const store = useUserStore()
        ElMessage({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        if (error && error.response && error.response.status === 401) {
            store.logout().then(() =>{
                location.reload()
            })
        }
        // for debug
        return Promise.reject(error)
    }
)
export const postActionFormData = (url:string, data:any) => {
    return service({
        url,
        method: 'post',
        transformRequest: [function (data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded' // 设置Content-Type头部信息为表单形式
        },
        data,
        params: data
    })
}
export const postAction = (url:string, data:any, sourceObj = {}) => {
    const obj = Object.assign({ data: true, params: true }, sourceObj)
    return service({
        url,
        method: 'post',
        data: obj.data ? data : undefined,
        params: obj.params ? data : undefined
    })
}
export const getAction = (url:string, data:any, sourceObj = {}) => {
    const obj = Object.assign({ data: true, params: true }, sourceObj)
    return service({
        url,
        method: 'get',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: obj.data ? data : undefined,
        params: obj.params ? data : undefined
    })
}
export const getActionExport = (url:string, data:any) => {
    return service({
        url,
        method: 'get',
        params: data,
        responseType: 'blob'
    })
}
export const putAction = (url:string, data:any, sourceObj = {}) => {
    const obj = Object.assign({ data: true, params: true }, sourceObj)
    return service({
        url,
        method: 'put',
        data: obj.data ? data : undefined,
        params: obj.params ? data : undefined
    })
}
export const deleteAction = (url:string, data:any, sourceObj = {}) => {
    const obj = Object.assign({ data: true, params: true }, sourceObj)
    return service({
        url,
        method: 'delete',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: obj.data ? data : undefined,
        params: obj.params ? data : undefined
    })
}
export default service
